package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbMoment;
import com.jftone.bh.app.model.NtbMomentComment;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.model.TmpImg;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.List;


@Service
public class MomentService {
	
	private Logger logger = Logger.getLogger(MomentService.class);
	
	@Autowired
	private Dao dao;

	@Autowired
	private UserService userService;

	@Autowired
	private OSSFileManagerService ossService;

	public List<IData<String, Object>> queryPageList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> list = null;
		String userId = data.getString("userId", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		String status = data.getString("status", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID", userId);
		}
		if(!StringUtil.isBlank(status)){
			param.put("STATUS", status);
		}
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", DateUtil.getMillisecond(DateUtil.parseDate(sTime)));
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", DateUtil.getMillisecond(DateUtil.parseDate(eTime)));
		}
		list = dao.query("queryMomentList", param, page);
		NtbUser user;
		for (IData<String, Object> moment:list) {
			String content = moment.getString("CONTENT","");
			if(moment.getInt("USER_ID",0) != 0){
				user = userService.getNtbUser(moment.getInt("USER_ID",0));
				if(user != null){
					moment.put("USER_NAME",user.getUserName());
				}
			}
			if(!StringUtil.isEmpty(content)){
				JSONArray ja = JSONArray.fromObject(content);
				moment.put("IMG_NUM",ja.size());
			}else{
				moment.put("IMG_NUM",0);
			}
		}
		return list;
	}


	public IData<String, Object> getNtbMoment(IData<String, Object> inData) throws DaoException {
		IData<String, Object> moment = new DataMap<String, Object>();
		NtbMoment momentObj = dao.get(NtbMoment.class,inData);
		if(momentObj!=null){
			moment.put("id",momentObj.getId());
			moment.put("preface",momentObj.getPreface());
			inData.clear();
			if(momentObj.getUserId() != 0){
				NtbUser user = userService.getNtbUser(momentObj.getUserId());
				if(user != null){
					moment.put("userName",user.getUserName());
					moment.put("userId",user.getId());
				}
			}
			moment.put("status",momentObj.getStatus());
			moment.put("detailImgHtml","");
			if(!StringUtil.isEmpty(momentObj.getContent())){
				JSONArray ja = JSONArray.fromObject(momentObj.getContent());
				String ossUrl = OSSFileManagerService.host;
				if(ja.size()>0){
					StringBuffer stringBuffer = new StringBuffer();
					for (int i=0 ;i<ja.size();i++) {
						JSONObject obj = ja.getJSONObject(i);
						stringBuffer.append("<img src='");
						stringBuffer.append(ossUrl+"/");
						stringBuffer.append(obj.getString("url"));
						stringBuffer.append("' srcImg= '");
						stringBuffer.append(obj.getString("url"));
						stringBuffer.append("'style='width:20%;height: 200px;'/>");
					}
					moment.put("detailImgHtml",stringBuffer.toString());
				}
			}
		}
		return moment;
	}

	@Transactional
	public int saveModel(IData<String, Object> data, boolean updateFlag) throws DaoException {

		NtbMoment obj = new NtbMoment();

		String saveImgSrc = data.getString("saveImgSrc","");
		IData<String, Object> updateData = new DataMap<String, Object>();
		IData<String,Object> whereData = new DataMap<String,Object>();
		if(!StringUtil.isEmpty(saveImgSrc)){
			String[] saveImgSrcArr = saveImgSrc.split(";");
			JSONArray ja = new JSONArray();
			for (String imgSrc:saveImgSrcArr) {
				if(!StringUtil.isEmpty(imgSrc)){
					JSONObject img = new JSONObject();
					img.put("url",imgSrc);
					ja.add(img);
					updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_USE);
					whereData.put("imgSrc", imgSrc);
					dao.update(TmpImg.class, updateData, whereData);
				}
			}
			obj.setContent(ja.toString());
		}else{
			obj.setContent("");
		}

		String delImgSrc = data.getString("delImgSrc","");
		if(!StringUtil.isEmpty(delImgSrc)){
			String[] delImgSrcArr = delImgSrc.split(";");
			for (String imgSrc:delImgSrcArr) {
				if(!StringUtil.isEmpty(imgSrc)){
					updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_YES);
					whereData.put("imgSrc", imgSrc);
					dao.update(TmpImg.class, updateData, whereData);
					ossService.delete(imgSrc);
				}
			}
		}
		obj.setStatus(data.getShort("status",SysConst.MOMENT_STATUS_0));
		obj.setPreface(data.getString("preface",""));
		if(updateFlag){
			obj.setId(data.getLong("momentId"));
			dao.update(obj);
		}else{
			obj.setUserId(data.getInt("userId",0));
			obj.setCreateTime(DateUtil.getMillisecond());
			dao.save(obj);
		}

		return 0;
	}
	@Transactional
	public int delete(long momentId) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",momentId);
		NtbMoment momentObj = dao.get(NtbMoment.class,param);
		if(momentObj == null){
			return 1;
		}

		if(!StringUtil.isEmpty(momentObj.getContent())){
			JSONArray ja = JSONArray.fromObject(momentObj.getContent());
			if(ja.size()>0){
				IData<String, Object> updateData = new DataMap<String, Object>();
				IData<String,Object> whereData = new DataMap<String,Object>();
				for (int i=0 ;i<ja.size();i++) {
					JSONObject obj = ja.getJSONObject(i);
					String imgSrc = obj.getString("url");
					if(!StringUtil.isEmpty(imgSrc)){
						updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_YES);
						whereData.put("imgSrc", imgSrc);
						dao.update(TmpImg.class, updateData, whereData);
						ossService.delete(imgSrc);
					}
				}
			}
		}
		param.clear();
		param.put("momentId",momentId);
		dao.delete(NtbMomentComment.class,param);
		dao.delete(momentObj);
		return 0;
	}
}
